$(function () {
    var pageNum =1;
    getTypeList(1);
    $('body').on('click','.select-list',function(){
        $("input[name='name']").val("");
          getTypeList(1);
    }).on('click','#btn-type,#btn-type2',function(){
        getTypeList(1);
    }).on("click","#new-tag",function(){
        saveType();
    }).on("click",".fa-edit",function(){
        saveType($(this).parents("a").data("id"));
    }).on("click","#delete-batch",function(){
        var obj = $("#oc-ibox-type-content input.i-checks:checked");
        var tagIds = obtainIds(obj);
        if($.isEmptyObject(tagIds) || tagIds == ""){
            icModale(0,"请选择要操作的数据");
        }else {
            icModale(2,"确定要删除所选数据吗？",3,function () {
                updateTypeStatus(tagIds, 3,null);
            });
        }
    }).on("click","#disable-batch",function(){
        var obj = $("#oc-ibox-type-content input.i-checks:checked");
        var tagIds = obtainIds(obj);
        if($.isEmptyObject(tagIds) || tagIds == ""){
            icModale(0,"请选择要操作的数据");
        }else {
            icModale(2,"确定要禁用所选数据吗？",3,function () {
                updateTypeStatus(tagIds, 2,null);
            });
        }
    }).on("click","#enable-batch",function(){
        var obj = $("#oc-ibox-type-content input.i-checks:checked");
        var tagIds = obtainIds(obj);
        if($.isEmptyObject(tagIds) || tagIds == ""){
            icModale(0,"请选择要操作的数据");
        }else {
            icModale(2,"确定要恢复所选数据吗？",3,function () {
                updateTypeStatus(tagIds, 1,null);
            });
        }
    }).on("click","#oc-ibox-type-content .fa-unlock-alt",function(){
       var id = $(this).parents("a").data("id");
        icModale(2,"确定要禁用所选数据吗？",3,function () {
            updateTypeStatus(id, 2,this);
        });
    }).on("click","#oc-ibox-type-content .fa-history",function(){
        var id = $(this).parents("a").data("id");
        icModale(2,"确定要恢复所选数据吗？",3,function () {
            updateTypeStatus(id, 1,this);
        });
    }).on("click","#oc-ibox-type-content .fa-trash-o",function(){
        var id = $(this).parents("a").data("id");
        icModale(2,"确定要删除所选数据吗？",3,function () {
            updateTypeStatus(id, 3,this);
        });
    }).on("click",'#advSearchshowBtn',function(){
        document.getElementById("search-form").reset();
    });
    /**
     * 查询问答分类列表
     * @param pageNo
     */
    function getTypeList(pageNo) {
        var params = $(".search-form-type").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/classes/topic/type/typeAjaxList',//请求URLt
            contextId: 'oc-ibox-type-content',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getTypeList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-tag', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#type-count").html(total);
            }
        };
        ajaxGetListData(obj);
    }

    function saveType(id) {
        parent.layer.open({
            type: 1,
            area: ['380px'],
            skin: 'layui-layer-demo',
            title: id!=null?'修改类型':'新建类型',
            content: '<div class="m-t m-r m-l m-b"><input class="form-control" id="typeName" type="text"></div>',
            btn: '确定',
            yes: function(index, layero) {
                var typeName = layero.find('#typeName').val();
                $.ajax({
                    url:baseloaction+"/admin/classes/topic/type/saveTopicType",
                    type:"post",
                    data:{"name":typeName,"id":id},
                    dataType:"json",
                    success:function (result) {
                        if (result.success == true) {
                            icModale(0,result.message)
                            getTypeList(1);
                            $("#type-count").html(parseInt($("#type-count").html()) + 1);
                        } else {
                            icModale(0,result.message);
                        }
                        parent.layer.close(index);
                    },
                    error:function (error) {
                        icModale(0,"系统繁忙，请稍后重试");
                    }
                });
            }
        });
    }


    function updateTypeStatus(ids,status,obj) {
        $.ajax({
            url:baseloaction+"/admin/classes/topic/type/updateTopicTypeStatus",
            type:"post",
            data:{"ids":ids,"status":status},
            dataType:"json",
            success:function (result) {
                if (result.success == true) {
                    icModale(0,result.message)
                    if(obj==null){
                        getTypeList(pageNum);
                    }else{
                        var tr = $('tr[data-id=' + ids + ']');
                        var tdThree = tr.find("td")[3];
                        var tdfive = tr.find("td")[5];
                        if(status==3){
                            tr.remove();
                        }else if(status==2){
                            $(tdThree).find('span').removeClass('label-info').addClass("label-danger").text("禁用");
                            var fg=$(tdfive).find('.fa-unlock-alt');
                            fg.parents('a').data("data-original-title","恢复");
                            fg.removeClass("fa-unlock-alt").addClass("fa-history");
                        }else{
                            $(tdThree).find('span').removeClass('label-danger').addClass("label-info").text("正常");
                            var fg=$(tdfive).find('.fa-history');
                            fg.parents("a").data("data-original-title","禁用");
                            fg.removeClass("fa-history").addClass("fa-unlock-alt");
                        }
                    }
                } else {
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,"系统繁忙，请稍后重试");
            }
        });
    }



});

//拼接id串
function obtainIds(obj) {
    var qaTypeIds = "";
    $(obj).each(function (index,_input) {
        qaTypeIds += $(_input).val() + ",";
    })
    qaTypeIds = qaTypeIds.substring(0,qaTypeIds.length-1);
    return qaTypeIds;
}